package cn.ze.yygh.order.api;


import cn.ze.yygh.common.result.Result;
import cn.ze.yygh.model.order.OrderInfo;
import cn.ze.yygh.order.service.OrderService;
import cn.ze.yygh.order.service.PaymentService;
import cn.ze.yygh.order.service.WeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private OrderService orderService;


    //生成微信二维码
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId){
        Map<String , Object> map = weixinService.createNative(orderId);
        return Result.ok(map);
    }


    //查询支付订单  未实际使用
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId){
       /*
        //调用微信接口实现查询支付状态
        Map<String , String> resultMap = weixinService.queryPayStatus(orderId);
        //判断
        if(resultMap == null ){
            return Result.fail().message("支付出错");
        }
        if("SUCCESS".equals(resultMap.get("trade_state"))){
            //支付成功 更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");
            paymentService.paySuccess(out_trade_no,resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");

        */

        //微信支付未实装  直接返回成功
        OrderInfo orderInfo = orderService.getById(orderId);
        orderInfo.setOrderStatus(1);
        orderService.updateById(orderInfo);

        return Result.ok().message("支付成功");
    }


}
